package com.cjh.CH2;

/**
 * @author ChenJunhong
 * @date 2018/11/23
 * Description:
 */
public class BadSuspend {
    public static Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");

    public static class ChangeObjectThread extends Thread {
        public ChangeObjectThread(String name) {
            super.setName(name);
        }

        @Override
        public void run() {
            synchronized (u) {
                System.out.println("suspend " + getName());
                Thread.currentThread().suspend();
                System.out.println("resume " + getName());
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        t1.start();
        Thread.sleep(100);
        // 此时t2不能获取到锁，t1占有锁
        t2.start();
        // t1释放恢复，释放锁，被t2获取到，t2运行后进入挂起状态并获取到了锁
        t1.resume();
        // 锁已经被获取了，不能导致不能进入，继续执行之后的代码
        t2.resume();
        t1.join();
        t2.join();
    }


}
